import 'package:get/get.dart';


class AddressSelectLogic extends GetxController {
  //当前选中的省份/地区
  String? provinceStr;
  //省份列表
  List<String> provinceList = [];

  //当前选中的市
  String? cityStr;
  //市级列表
  List<String> cityList = [];

  //当前选中的区/县
  String? countyStr;
  //区县列表
  List<String> countyList = [];

  //当前地区选择类型 1：省 2：市 3：区县
  AddressType addressType = AddressType.province;
  List<String> addressList = [];

  @override
  void onInit() {
    super.onInit();
    provinceList.add('北京市');
    provinceList.add('天津市');
    provinceList.add('河北省');
    provinceList.add('陕西省');

    cityList.add('保定市');
    cityList.add('沧州市');
    cityList.add('承德市');
    cityList.add('邯郸市');

    countyList.add('安国');
    countyList.add('定州市');
    countyList.add('定兴县');
    countyList.add('高阳县');

    addressList.addAll(provinceList);
  }

  //当前地址选择弹窗选择类型 1:省份 2：市级  3：区县
  void changeAddressType(AddressType type) {
    addressType = type;
    update();
    changeAddressList();
  }

//设置当前选中的地址相
  void setAddressStr(String address) {
    if (addressType == AddressType.province) {
      addressType = AddressType.city;
      provinceStr = address;
      cityStr = null;
      countyStr = null;
    } else if (addressType == AddressType.city) {
      addressType = AddressType.area;
      cityStr = address;
      countyStr = null;
    } else if (addressType == AddressType.area) {
      countyStr = address;
    }
    update();
    changeAddressList();
  }


  void changeAddressList() {
    addressList.clear();
    if (addressType == AddressType.province) {
      addressList.addAll(provinceList);
    } else if (addressType == AddressType.city) {
      addressList.addAll(cityList);
    } else if (addressType == AddressType.area) {
      addressList.addAll(countyList);
    }
    update();
  }
}

enum AddressType {
  province,
  city,
  area
}